<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css" />
<link href="${pageContext.request.contextPath}/css/table.css" rel="stylesheet" type="text/css" />
<link href="${pageContext.request.contextPath}/dwz/themes/default/style.css" rel="stylesheet" type="text/css" media="screen" />
<link href="${pageContext.request.contextPath}/dwz/themes/css/core.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery/jquery-2.1.4.min.js"></script>
<table border="0" cellpadding="0" cellspacing="0" class="table_border">
	<tr>
		<td>
			<table width="100%" border="0" cellpadding="0" cellspacing="0"
				class="table_right">
				<tr>
					<td><font style="font-size: 12px;"><strong>新增许可</strong></font>
					</td>
					<td width="25" height="26" align="left"></td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td>
			<form id="add_permission">
				<input type="text" id="pId" name="pId" value="" style="display:none;" />
				<table width="100%" border="0" cellspacing="0" class="table_right">
					<tr>
						<td align="center">
							<table border="0" cellpadding="0" cellspacing="0"
								class="box_table" id="box_table2">
								<thead>
									<tr>
										<td height="36" class="box_table_even">代码：</td>
										<td class="box_table_odd"><input id="code" type="text" name="code" />
										<span style="color: red"  id="code_msg" >*</span></td>
									</tr>
									<tr>
										<td height="36" class="box_table_even">名称：</td>
										<td class="box_table_odd"><input type="text" id="name" name="name" />
										<span style="color: red"  id="name_msg">*</span></td>
									</tr>
									<tr>
										<td class="box_table_even">模块URL：</td>
										<td class="box_table_odd"><input type="text" name="moduleUrl" /></td>
									</tr>
									<tr>
										<td class="box_table_even">操作URL：</td>
										<td class="box_table_odd"><input type="text" size="90" name="operationUrl" />
											多个逗号隔开</td>
									</tr>
									<tr>
										<td class="box_table_even">排序号：</td>
										<td class="box_table_odd"><input type="text" name="orderNo" /></td>
									</tr>
								</thead>
								<tbody>
								</tbody>
							</table>

							<table border="0" cellpadding="0" cellspacing="0"
								class="operation">
								<thead>
									<tr>
										<td height="24">&nbsp;</td>
									</tr>
								</thead>
							</table>
						</td>
					</tr>
				</table>
			</form>
		</td>
	</tr>
	<tr>
		<td>
			<table width="100%" border="0" cellpadding="0" cellspacing="0"
				class="table_padding">
				<tr>
					<td height="21" align="right">
						<table align="left">
							<tr>
								<td width="50px"><a id="addPermission_btn" class="button" ><span>保存</span></a></td>
								<td width="50px"><a class="button" href="javascript:void(window.history.back());"><span>返回</span></a></td>
								<td style="color: red" id="msg"></td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>

<script>

	//获取表单的json对象
	function getParame(curform) {
		let serializeArray = curform.serializeArray();
		var parame = new Object();

		for (let i = 0; i < serializeArray.length; i++) {
			let name = serializeArray[i].name;
			let value = serializeArray[i].value;
			parame[name] = value;
		}
		return parame;
	}
	var flagg;
	//判断code是否重复
	function checkCode(){
		let val = $("#code").val();
		$.post("checkCode",{code:val},function (data) {
			$("#code_msg").text(data.errorMsg);

			if(data.status==true){
				$("#code_msg").prop("style","color: green");
				flagg = true;
			}else {
				$("#code_msg").prop("style","color: red");
				flagg = false;
			}
		});
		return flagg;
	}


	//验证姓名不能有特殊字符
	function cheakName(){
		let val = $("#name").val();
		var flag = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>《》/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？ ]")
		var _reg = /^\S{4,20}$/;
		if(!flag.test(val) && !_reg.test(val)){
			$("#name_msg").text("名字不能包含特殊字符且长度4-20");
			$("#name_msg").prop("style","color: red");
			return false;
		}else {
			$("#name_msg").text("名字可用");
			$("#name_msg").prop("style","color: green");
			return true;
		}
	}

	//发送异步请求判断code是否重复
	$("#code").blur(checkCode);
	$("#name").blur(cheakName);

	//点击保存按钮的时候发送异步请求保存权限
	$("#addPermission_btn").click(function () {
		//验证数据是否正确
		if(checkCode() && cheakName()){
			$("#msg").text("添加中...");
			//获取树节点
			let zTreeObj = parent.treeFrame.$.fn.zTree.getZTreeObj("permission_tree");
			var nodes = zTreeObj.getSelectedNodes();
			$("#pId").val(nodes[0].id);
			//获取表单数据
			let aa = $("#add_permission");
			//发送异步请求
			$.post("addPermission",getParame(aa),function (data) {
				$("#msg").text(data.errorMsg);
				$("#name_msg").text(data.t.name);
				//判断是否添加成功8
				if(data.status==true){
					zTreeObj.addNodes(nodes[0], data.t);
				}
			});
		}else {
			$("#msg").text("请输入合法数据");
		}
	})
</script>
